// pages/components/backToBtn/backToBtn.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    // 路徑
    url: {            // 属性名
      type: String,     // 类型（必填），目前接受的类型包括：String, Number, Boolean, Object, Array, null（表示任意类型）
      value: '/pages/index/index'     // 属性初始值（可选），如果未指定则会根据类型选择一个
    },
    title: {
      type: String,
      value: ''
    },
    type:{
      type: String,
      value :'tab'
    },
    isType:{
      type: Boolean,
      value :false
    },
    buttonRight:{
      type:Number,
      value:0
    },
    buttonBottom:{
      type:Number,
      value:270
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    buttonTop: 444,
    buttonLeft: 0,
    windowHeight: 0,
    windowWidth: 0,
    startPoint:'',
    showIndex:false
  },
  /**
   * 生命周期
   */
  lifetimes: {
    created() {
      let that = this
      wx.getSystemInfo({
        success: (res) => {
          // 高度,宽度 单位为px
          this.data.windowHeight = res.windowHeight;
          let param = {
            windowHeight: res.windowHeight,
            windowWidth: res.windowWidth,
            buttonLeft:res.windowWidth - that.data.buttonRight,
            buttonTop:res.windowHeight - that.data.buttonBottom
          }
          this.data = {...this.data,param}
         
        }
      });
    },
    attached(){
      let page = getCurrentPages();
      let { showIndex } = this.data;
      if(page.length==1||this.properties.isType){
        showIndex=true;
      }
      this.setData({
        showIndex
      });
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    router(e) {
      if(this.data.type=='tab'){
        wx.switchTab({
          url: this.properties.url
        })
      }else{
        wx.navigateTo({
          url: this.properties.url
        })
      }
   

    },
    buttonStart(e){
      this.setData({
        startPoint:e.changedTouches[0]
      })
    },
    buttonMove(e){
   this.setData({
    canMove:true
   })
    let endPoint = e.changedTouches[e.changedTouches.length - 1];
    let { startPoint } = this.data;
    let translateX = endPoint.clientX - startPoint.clientX
    let translateY = endPoint.clientY - startPoint.clientY
   console.log(endPoint)
    this.setData({
      startPoint:endPoint
    })
    var buttonTop = this.data.buttonTop + translateY
    var buttonLeft = this.data.buttonLeft + translateX
    //判断是移动否超出屏幕
   if (buttonLeft+67 >= this.data.windowWidth){
      buttonLeft = this.data.windowWidth-67;
    }
    if (buttonLeft<=0){
      buttonLeft=0;
    }
    if (buttonTop<=0){
      buttonTop=0
    }
    if (buttonTop + 67 >= this.data.windowHeight){
      buttonTop = this.data.windowHeight-67;
    }
    this.setData({
      buttonTop: buttonTop,
      buttonLeft: buttonLeft
    })

    }
  }
})
